package com.kele.unsafe;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author kele
 * @date 2021/10/9 18:06
 * @description
 */

public class SetTest {

    public static void main(String[] args) {

        /**
         * 并发环境下用set会有并发的异常
         */
       //Set<String> set=new HashSet<>();
        //Set<String> set= Collections.synchronizedSet(new HashSet<>());
        Set<String> set=new CopyOnWriteArraySet<>();
        for (int i = 0; i <30 ; i++) {
            new  Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set );
            },String.valueOf(i)).start();



        }
    }
}
